package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.json.JSONArray;
import com.ruoyi.common.config.PersonPosition;
import com.ruoyi.system.services.EnclosureServices;
import com.ruoyi.system.services.PersonPositionServices;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.EnclosureInfo;
import com.ruoyi.system.service.IEnclosureInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 围栏信息Controller
 *
 * http://localhost:38080/enclosure/findAll
 * http://localhost:38080/enclosure/token
 * http://localhost:38080/enclosure/count
 *
 * @author ruoyi
 * @date 2025-09-11
 */
@RestController
@RequestMapping("/enclosure")
public class EnclosureInfoController extends BaseController
{
    @Autowired
    private EnclosureServices enclosureServices;
    @Autowired
    private IEnclosureInfoService enclosureInfoService;
    @Autowired
    private PersonPositionServices personPositionServices;

    /**
     * 查询围栏信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(EnclosureInfo enclosureInfo)
    {
        startPage();
        List<EnclosureInfo> list = enclosureInfoService.selectEnclosureInfoList(enclosureInfo);
        return getDataTable(list);
    }

    /**
     * 导出围栏信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "围栏信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EnclosureInfo enclosureInfo)
    {
        List<EnclosureInfo> list = enclosureInfoService.selectEnclosureInfoList(enclosureInfo);
        ExcelUtil<EnclosureInfo> util = new ExcelUtil<EnclosureInfo>(EnclosureInfo.class);
        util.exportExcel(response, list, "围栏信息数据");
    }

    /**
     * 获取围栏信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(enclosureInfoService.selectEnclosureInfoById(id));
    }

    /**
     * 新增围栏信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "围栏信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EnclosureInfo enclosureInfo)
    {
        return toAjax(enclosureInfoService.insertEnclosureInfo(enclosureInfo));
    }

    /**
     * 修改围栏信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "围栏信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EnclosureInfo enclosureInfo)
    {
        return toAjax(enclosureInfoService.updateEnclosureInfo(enclosureInfo));
    }

    /**
     * 删除围栏信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "围栏信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(enclosureInfoService.deleteEnclosureInfoByIds(ids));
    }










/*********************************** 人员定位系统-围栏报警数据 ***************************************/
    @GetMapping("/token")
    public AjaxResult getToken() {
        return AjaxResult.success(PersonPosition.getToken());
    }

    @GetMapping("/count")
    public AjaxResult getCount() {
        return AjaxResult.success(PersonPosition.count());
    }

    @GetMapping("/all/areas")
    public AjaxResult getAllArea() {
        JSONArray result = enclosureServices.getAllArea();

        return AjaxResult.success(result);
    }

    /**
     * 编辑数据
     * 围栏模型: car-车辆报警 dangerous-危险源报警 staff-人员报警
     * @return 编辑结果
     */
    @GetMapping("/findAll")
    public AjaxResult getEnclosureList(Integer fenceId, String name) {
        String result = enclosureServices.getEnclosureList(fenceId, name);

        return AjaxResult.success(result);
    }

    /**
     *
     * @param type   员工类型:1.内部 2.访客 3.承包商
     * @param pageSize
     * @return
     */
    @GetMapping("/user/{type}/{pageSize}")
    @ApiOperation("用户列表")
    public AjaxResult staffList(@PathVariable("type") Integer type, @PathVariable("pageSize") Integer pageSize) {
        return AjaxResult.success(personPositionServices.getStaffUserList(type, pageSize));
    }

    /**
     * 查询订阅
     *
     * @return 订阅信息
     */
    @GetMapping("/subscribeList")
    public AjaxResult subscribeList() {
        String result = enclosureServices.subscribeList();

        return AjaxResult.success(result);
    }
/*********************************** 人员定位系统-围栏报警数据 ***************************************/

}
